namespace Maple.Branch.Identity.EfCoreRepositories
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Linq.Dynamic.Core;
    using System.Threading.Tasks;
    using EfCoreConfigurations;
    using Entities;
    using IRepositories;
    using Microsoft.EntityFrameworkCore;
    using Volo.Abp.Domain.Repositories.EntityFrameworkCore;
    using Volo.Abp.EntityFrameworkCore;

    public class IdentityClaimTypeRepository : EfCoreRepository<IIdentityDbContext, IdentityClaimType, Guid>, IIdentityClaimTypeRepository
    {
        public IdentityClaimTypeRepository(IDbContextProvider<IIdentityDbContext> dbContextProvider)
            : base(dbContextProvider)
        {

        }

        public virtual async Task<bool> AnyAsync(string name, Guid? ignoredId = null)
        {
            return await DbSet
                       .WhereIf(ignoredId != null, ct => ct.Id != ignoredId)
                       .CountAsync(ct => ct.Name == name) > 0;
        }

        public virtual async Task<List<IdentityClaimType>> GetListAsync(string sorting, int maxResultCount, int skipCount, string filter)
        {
            var identityClaimTypes = await DbSet
                .WhereIf(
                    !filter.IsNullOrWhiteSpace(),
                    u =>
                        u.Name.Contains(filter)
                )
                .OrderBy(sorting ?? "name desc")
                .PageBy(skipCount, maxResultCount)
                .ToListAsync();

            return identityClaimTypes;
        }
    }
}
